import 'package:airplay_plus/AirPlayWidget.dart';
import 'package:flutter/material.dart';
import 'dart:async';

import 'package:airplay_plus/airplay_plus.dart';
import 'package:mybase/base/widget/layout/FFLineContainer.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String statusStr = '未连接';
  final _airplayPlusPlugin = AirplayPlus();

  StreamSubscription? sub;

  @override
  void initState() {
    super.initState();

    sub = _airplayPlusPlugin.getAirPlayStatusStream().stream.listen((open) {
      statusStr = open ? "已连接" : "未连接";
      print("flutter 收到的状态是 $statusStr");
      setState(() {});
    });
  }

  @override
  void dispose() {
    sub?.cancel();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text(statusStr),
        ),
        body: FFLineContainer(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            AirPlayWidget(
              disConnectWidget: Container(
                height: 60,
                width: 60,
                color: Colors.red,
              ),
              connectWidget: Container(
                height: 60,
                width: 60,
                color: Colors.green,
              ),
            ),
          ],
        ),
        floatingActionButton: GestureDetector(
          child: Container(
            height: 160,
            width: 160,
            color: Colors.pink,
            alignment: Alignment.center,
            child: const Text("打开面板"),
          ),
          onTap: () async {
            await _airplayPlusPlugin.airPlay();
          },
        ),
      ),
    );
  }
}
